<template>
    <div>
        <el-dialog
                :title="isEdit ? '编辑' : '新增'"
                :visible.sync="dialogVisible"
                width="50%"
                :before-close="handleClose">
            <el-form v-if="isEdit" label-position="left" :rules="formRules.departmentFrom" ref="departmentForm"
                     label-width="80px" :model="newsData">
                <el-form-item label="小组名称">
                    <el-input v-model="newsData.groupname" style="width: auto" :disabled="true"/>
                </el-form-item>

                <el-form-item label="消息内容">
                    <el-input type="textarea" v-model="newsData.content" style="width: auto"/>
                </el-form-item>
            </el-form>

<!--            新增-->
            <el-form v-else label-position="left" :rules="formRules.departmentFrom" ref="departmentForm"
                     label-width="80px" :model="addNewForm">
                <el-form-item label="小组名称">
                    <template>
                        <el-select v-model="addNewForm.groupid" placeholder="小组名称">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.groupName"
                                    :value="item.groupid">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>

                <el-form-item label="消息内容">
                    <el-input type="textarea" v-model="addNewForm.content" style="width: auto"/>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
    		<el-button type="primary" @click="onSubmit" v-loading="loading">确 定</el-button>
  		</span>
        </el-dialog>
    </div>
</template>

<script>
    import * as teacher from '@/api/teacher'
    import * as classManage from '@/api/classManage';
    import * as groupManage from '@/api/groupManage';
    import * as newsManage from '@/api/newsManage';
    import Cookie from "js-cookie";

    export default {
        name: 'addDialog',
        props: {
            dialogVisible: {
                type: Boolean,
                default: false
            },
            isEdit: {
                type: Boolean
            },
            newsData: {
                type: Object,
                default: {}
            }
        },
        data() {
            return {
                loading: false,
                majorForm: {
                    departmentid: '',
                    majorname: ''
                },
                addNewForm: {
                    content: '',
                    groupid: ''
                },
                editReplayScoreForm:{
                    scoreid: '',
                    replayscore: ''
                },
                editReportScoreForm:{
                    scoreid: '',
                    reportscore: ''
                },
                options: [],
                textarea: ''
            }
        },
        methods: {
            onSubmit() {
                if (this.isEdit) {
                    this.onEditNews();
                } else {
                    this.onAddNews();
                }
            },
            async onAddNews() {
                try {
                    this.loading = true;
                    await newsManage.addNews(this.addNewForm);
                    this.$notify({
                        title: '信息',
                        type: 'success',
                        message: '添加成功!'
                    });
                    this.handleFresh();
                    this.handleClose();
                } catch (e) {
                    this.$notify({
                        title: '错误',
                        type: 'error',
                        message: '添加失败!'
                    });
                    console.log(e)
                } finally {
                    this.loading = false;
                }
            },
            async onEditNews() {
                try {
                    this.loading = true;
                    await newsManage.editNews(this.newsData);
                    this.$notify({
                        title: '信息',
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.handleFresh();
                    this.handleClose();
                } catch (e) {
                    this.$notify({
                        title: '错误',
                        type: 'error',
                        message: '修改失败!'
                    });
                    console.log(e)
                } finally {
                    this.loading = false;
                }
            },
            async getGroupList() {
                console.log(this.newsData);
                let teacherID = JSON.parse(Cookie.get("teacherid"));
                const res = await groupManage.fetchGroupList({teacherid : teacherID});
                this.options = res.msg
            },
            handleClose() {
                this.$emit('closeDialog');
            },
            handleFresh() {
                this.$emit('fresh');
            }
        },
        watch: {
            dialogVisible(newVal, oldVal) {
                if (newVal) {
                    if (!this.isEdit) {
                        this.majorForm = {
                            departmentid: '',
                            majorname: ''
                        }
                    }
                }
            }
        },
        created() {
            this.addSubjectFrom= {
                subjectname: '',
                teacherid: '',
                requires: ''
            };
            this.getGroupList();
        }
    }
</script>

<style scoped>

</style>